package com.dlsy.ygj.dao.vindicateData.impl;

import java.sql.SQLException;
import java.util.List;

import net.taobaoge.common.framework.dao.impl.BaseDAOImpl;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.util.CollectionUtils;

import com.dlsy.ygj.dao.vindicateData.IRoomTypeDAO;
import com.dlsy.ygj.entity.Department;
import com.dlsy.ygj.entity.RoomType;

public class RoomTypeDAO extends BaseDAOImpl<RoomType> implements IRoomTypeDAO {

	@SuppressWarnings("unchecked")
	public List<RoomType> findRoomTypeByProperty(String value, Integer stroreId) {
		Criteria criteria = getSession().createCriteria(Department.class);
		criteria.add(Restrictions.and(Restrictions.like("roomTypeName", "%"
				+ value + "%"), Restrictions.eq("storefrontId", stroreId)));
		List<RoomType> roomTypes = criteria.list();
		if (roomTypes != null && roomTypes.size() > 0) {
			return roomTypes;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<RoomType> checkExist(final Integer roomTypeId,
			final String value, final Integer stroreId) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public List<RoomType> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = getSession().createCriteria(RoomType.class);
				criteria.add(Restrictions.and(Restrictions.and( Restrictions.eq(
								"roomTypeName", value), Restrictions
						.not(Restrictions.eq("roomTypeId", roomTypeId))),
						Restrictions.eq("storefrontId", stroreId)));
				List<RoomType> roomTypes = criteria.list();
				if (roomTypes != null && roomTypes.size() > 0) {
					return roomTypes;
				}
				return null;
			}
		});
	}

	public List<RoomType> findAll(Integer stroreId) {
		try {
			List<RoomType> roomTypes = findByProperty(RoomType.class,
					"storefrontId", stroreId);
			if (!CollectionUtils.isEmpty(roomTypes)) {
				return roomTypes;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<RoomType> checkExist(final String value, final Integer stroreId) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public List<RoomType> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = getSession().createCriteria(RoomType.class);
				criteria.add(Restrictions.and( Restrictions.eq("roomTypeName",
						value), Restrictions.eq("storefrontId", stroreId)));
				List<RoomType> roomTypes = criteria.list();
				if (roomTypes != null && roomTypes.size() > 0) {
					return roomTypes;
				}
				return null;
			}
		});
	}

}
